﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace MailServerStatus
{
    public partial class MailServer : Form
    {
        public MailServer()
        {
            InitializeComponent();
        }

        private void btnCheck_Click(object sender, EventArgs e)
        {
            try
            {
                
                lblError.Text = "";
                if (Validation())
                {
                    MailMessage message = new MailMessage();
                    message.To.Add("test@mail.com");
                    message.From=new MailAddress(txtUserName.Text);
                    SmtpClient client = new SmtpClient();
                    client.UseDefaultCredentials = false;
                    client.Host = txtServer.Text;
                    if (txtPort.Text != "")
                    {
                        client.Port = Convert.ToInt32(txtPort.Text);
                    }
                    if (chkSSL.Checked)
                    {
                        client.EnableSsl = true;
                    }
                    client.DeliveryMethod = SmtpDeliveryMethod.Network;
                    client.Credentials = new NetworkCredential(txtUserName.Text, txtPassword.Text);
                    client.Send(message);
                    txtOutput.Text = "Mail Server Is Active";
                }

            }
            catch (Exception ex)
            {
                ErrorPrinting(ex);
            }
            
        }

        public bool Validation()
        {

            if (string.IsNullOrEmpty(txtUserName.Text))
            {
                lblError.Text = "User Email Id Required";
                return false;
            }
            else if (string.IsNullOrEmpty(txtPassword.Text))
            {

                lblError.Text = "Password Required";
                return false;
            }
            else if (string.IsNullOrEmpty(txtServer.Text))
            {

                lblError.Text = "Mail Server Name Required";
                return false;
            }
            else if (string.IsNullOrEmpty(txtPort.Text))
            {

                lblError.Text = "Mail Server Name Required";
                return false;
            }
            else
            {
                lblError.Text = "";
                return true;
            }
        }
        public string ErrorPrinting(Exception ex)
        {
            if (string.IsNullOrEmpty(ex.InnerException.ToString()))
            {
               return txtOutput.Text = "Error : " + ex.Message;
            }
            else
            {
              return  txtOutput.Text = "Error : " + ex.Message + " , " + "InnerException :" + ex.InnerException.ToString();
            }
        }


    }
}
